package org.NooLab.docserver.frame;

 
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
 

import processing.core.*;



public abstract class AbstractApplet extends PApplet {
 
	private static final long serialVersionUID = 1263273736878519844L;
	
	protected static secondCanvas sCanvas;
	protected static AppletWindow wApplet;
	AppEventsIntf events;
	public AppletWindow applet;
	String caption ;
	int xpos=10,ypos=10 ;
	int width=280,	height = 240;
	
	
	public AbstractApplet( String caption, int x, int y,int w, int h ){
		xpos=x;
		ypos=y;
		int width= w;
		height = h;
		
		_init(caption);
	}
	
	public AbstractApplet( String caption, int x, int y ){
		xpos=x;
		ypos=y;
		 
		_init(caption);
	}
	
	public AbstractApplet( String caption ){
		
		_init(caption);
	}
	
	private void _init( String caption){ 
		
		this.caption = caption;
		// this would open an applet window
		wApplet = new AppletWindow();
		sCanvas = new secondCanvas( caption, wApplet,  xpos, ypos);
		
		// sCanvas.setVisible(true);
		
	}

	public void setEventsIntf( AppEventsIntf events){
		this.events = events;
		applet = wApplet;
	}
	
	public class AppletWindow extends PApplet {
 
		private static final long serialVersionUID = -459478981556688670L;

		public void setup() {
			 
			size( width, height);// ---numbers doesn't seem matter, change size inside
							// class secondCanvas
			// setLocation(xpos, ypos);
			background(127);
			
			// sCanvas = new secondCanvas( caption, wApplet, xpos, ypos);
		}

		// ---------this draws the second window--------------
		public void draw() {
			
			 
		}

		public void keyPressed() {
			// System.out.println("key pressed 2");
			events.keyPressed(key) ;
		}

		public void stop(){
			
			 
			
			super.stop();
		}
	}

		 
	public class secondCanvas extends Frame {
		
		public secondCanvas(String name, PApplet embed, int x, int y) {
			super(name);

			 
			// add the PApplet to the Frame
			setLayout(new BorderLayout());
			add(embed, BorderLayout.CENTER);

			// ensures that the animation thread is started and
			// that other internal variables are properly set.
			embed.init();

			// add an exit on close listener
			addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent evt) {
					// exit the application
					System.exit(0);
				}
			});
			
			setLocation(x, y);
			setSize(width, height);// 360 + 16, 120 + 38);// --add the size of the window
			
			setVisible(true);
		}
	}


	public static AppletWindow getApplet() {
		return wApplet;
	}

	public void setBackground(int r, int g, int b) {
		wApplet.background( r,g,b);
	}
	public void setBackground(int c) {
		wApplet.background( c,c,c);
	}
	
	public void setWindowSize( int w, int h){
		wApplet.width = w;
		wApplet.height = h;
		width = w;
		height = h;
		sCanvas.setSize(w,h);
	}
	
	public void setWindowSize( int x, int y, int w, int h){
		xpos = x;
		ypos = y;
		wApplet.width = w;
		wApplet.height = h;
		width = w;
		height = h;
		sCanvas.setSize(w,h);
		sCanvas.setLocation(x, y);
	}
	public void setWindowLocation(int x, int y){
		sCanvas.setLocation(x, y);
	} 
}
